<template>
  <div style="display: flex; justify-content: center; align-items: center; width: 100%; height: 24px; font-weight: bold; font-size: 24px; margin-top: 24px;">
    材料购买申请
  </div>
  <div class="form-container">
    <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
        class="demo-ruleForm"
    >
      <el-form-item label="材料名称" prop="materialName">
        <el-input v-model="ruleForm.materialName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="材料描述" prop="materialDescription">
        <el-input v-model="ruleForm.materialDescription" autocomplete="off" />
      </el-form-item>
      <el-form-item label="材料类别" prop="category">
        <el-input v-model="ruleForm.category" autocomplete="off" />
      </el-form-item>
      <el-form-item label="材料价格" prop="price">
        <el-input v-model.number="ruleForm.price" autocomplete="off" />
      </el-form-item>
      <el-form-item label="材料数量" prop="quantity">
        <el-input v-model.number="ruleForm.quantity" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          提交
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="bottom-fixed-button">
    <el-button type="primary" style="width: 90%; " @click="returnToTechnician">返回菜单</el-button>
  </div>
</template>

<script lang="ts" setup>
import axios from 'axios';
import { reactive, ref } from 'vue'
import {ElButton, FormInstance, FormRules} from 'element-plus'
import { ElMessage } from 'element-plus';
import {useRouter} from "vue-router";

const router = useRouter()
const returnToTechnician = () =>{
  router.push('/technician/home')
}
const ruleFormRef = ref<FormInstance>()

const validatePrice = (rule: any, value: number, callback: any) => {
  if (isNaN(value) || value <= 0) {
    callback(new Error('请输入有效的价格（大于0）'))
  } else {
    callback()
  }
}

const validateQuantity = (rule: any, value: number, callback: any) => {
  if (isNaN(value) || value <= 0) {
    callback(new Error('请输入有效的数量（大于0）'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  materialName: '',
  materialDescription:'',
  category:'',
  price: '',
  quantity: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  materialName: [
    { required: true, message: '请输入材料名称', trigger: 'blur' },
  ],
  price: [
    { validator: validatePrice, trigger: 'blur' },
  ],
  quantity: [
    { validator: validateQuantity, trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  try {
    await formEl.validate()
    const response = await axios.post('http://localhost:8005/mp/buy', {
      materialName: ruleForm.materialName,
      materialDescription: ruleForm.materialName,
      category: ruleForm.category,
      materialPrice: ruleForm.price,
      purchaseQuantity: ruleForm.quantity,
    })
    if (response.status === 200) {
      ElMessage.success('已成功向管理员提出申请');
      resetForm(formEl); // 清空表单
    } else {
      ElMessage.error('服务器响应状态不是 200');
    }
  } catch (error) {
    ElMessage.error('提交失败！');
  }
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>
.form-container {
  display: flex;
  justify-content: flex-start; /* 将内容对齐到左边 */
  margin-top: 40px; /* 保持顶部间距 */
}
.bottom-fixed-button {
  height: 50px;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  z-index: 1000;
  justify-content: center;
  align-items: center;
  padding-bottom: 10px;
}
</style>